package dice.sinanya.tools.makedata;

import dice.sinanya.entity.EntityReply;
import dice.sinanya.exceptions.ReplyTooShortException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.HashMap;

import static dice.sinanya.system.MessagesSystem.*;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entityGame;

public class ReplyFromXml {
    String text;

    public ReplyFromXml(String text) {
        if (text != null) {
            this.text = text;
        } else {
            this.text = "";
        }
    }


    public void flushMap() throws DocumentException, ReplyTooShortException {
        Document document = DocumentHelper.parseText(text);
        replyMap.clear();
        for (Object objectGroup : document.getRootElement().elements()) {
            getGroup(replyMap, objectGroup);
        }
        entityGame.setReply(text);
        new dice.sinanya.db.properties.game.InsertProperties().insertProperties(entityGame);
    }

    public void flushMapInRunning() throws DocumentException, ReplyTooShortException {
        Document document = DocumentHelper.parseText(text);
        for (Object objectGroup : document.getRootElement().elements()) {
            getGroup(replyMap, objectGroup);
        }
        entityGame.setReply(text);
    }

    public void flushAllMap(long qq) {
        HashMap<String, ArrayList<EntityReply>> replyAllHash = new HashMap<>();
        Document document;
        try {
            document = DocumentHelper.parseText(text);
        }catch (org.dom4j.DocumentException ignore){
            return;
        }
        for (Object objectGroup : document.getRootElement().elements()) {
                getAllGroup(replyAllHash, objectGroup);
        }
        AllReply.put(qq, replyAllHash);
    }

    private void getAllGroup(HashMap<String, ArrayList<EntityReply>> replyAllHash, Object objectGroup) {
        Element group = (Element) objectGroup;
        if (group.getName().equals("自身")) {
            return;
        }
        String key = group.attributeValue("关键字");
        ArrayList<EntityReply> replies = new ArrayList<>();
        for (Object objectValuesGroup : group.elements()) {
            Element valuesGroup = (Element) objectValuesGroup;
            if ("是".equals(valuesGroup.attributeValue("公共"))) {
                replies.add(getValues(objectValuesGroup));
            }
        }
        replyAllHash.put(key, replies);
    }

    private void getGroup(HashMap<String, ArrayList<EntityReply>> replyHash, Object objectGroup) throws ReplyTooShortException {
        Element group = (Element) objectGroup;
        if (group.getName().equals("自身")) {
            putSelf(replyHash, group, loginInfo.getLoginName());
            putSelf(replyHash, group, String.valueOf(loginInfo.getLoginId()));
            putSelf(replyHash, group, String.format("[CQ:at,qq=%s,display=@%s]", loginInfo.getLoginId(), loginInfo.getLoginName()));
            return;
        }
        String key = group.attributeValue("关键字");
        if (key.length() < 4) {
            throw new ReplyTooShortException();
        }
        ArrayList<EntityReply> replies = new ArrayList<>();
        for (Object objectValuesGroup : group.elements()) {
            replies.add(getValues(objectValuesGroup));
        }
        replyHash.put(key, replies);
    }

    private void putSelf(HashMap<String, ArrayList<EntityReply>> replyHash, Element group, String key) {
        ArrayList<EntityReply> replies = new ArrayList<>();
        EntityReply entityReply = new EntityReply();
        String value = group.getText();
        entityReply.setValue(value);
        entityReply.setWeight(1);
        replies.add(entityReply);
        replyHash.put(key, replies);
    }

    private EntityReply getValues(Object objectValuesGroup) {
        Element valuesGroup = (Element) objectValuesGroup;
        EntityReply entityReply = new EntityReply();

        int weight = 1;
        entityReply.setValue(valuesGroup.element("回复").getTextTrim());
        if (valuesGroup.element("权重") != null) {
            weight = Integer.parseInt(valuesGroup.element("权重").getTextTrim());
        }
        if ("是".equals(valuesGroup.attributeValue("公共"))){
            entityReply.setPublicMode(true);
        }

        entityReply.setWeight(weight);
        return entityReply;
    }
}
